<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="http://shiro.apache.org/tags"%>
<%@ include file="/WEB-INF/view/include/inc.jsp"%>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/WEB-INF/view/include/jQgrid.jsp"%>
</head>
<body>
	<div class="">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox " style="margin-bottom: 0px;">
					<div class="ibox-title" style="border-color: white;">
						<div class="row">
							<div class="col-sm-3">
								<div class="input-group">
									<input id="uname" type="text" placeholder="请输入关键词"
										class="input-sm form-control"> <span
										class="input-group-btn"> <a id="find_btn" type="button"
										class="btn btn-sm btn-primary"> 搜索</a>
									</span>
								</div>
							</div>
							<s:hasPermission name="functional:add">
								<div class="col-sm-3">
									<a id="add_btn" type="button" class="btn btn-sm btn-success"
										onclick="add()">新增</a> </span>
								</div>
							</s:hasPermission>
							<div class="col-sm-3">
									<span class="input-group-btn">
	                             		<a id="find_btn" type="button" class="btn btn-sm btn-success" style="float: right;" onclick="uploadxlsx();"> 导入数据</a>
	                             		<form class="form-horizontal m-t" id="signupForm" method="post" enctype="multipart/form-data" style="display: none;">
	                             			<input type="file" name="xlsfile" id="xlsfile"   accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel" style="display: none;"  onchange="inputExcel();" />
	                             		</form>
	                             	 </span>
								</div>
						</div>
					</div>
					<div class="ibox-content">

						<div class="jqGrid_wrapper">
							<table id="table_list"></table>
							<div id="pager_list"></div>
						</div>

					</div>
				</div>
			</div>
		</div>
	</div>
	<!-- Page-Level Scripts -->
	<script>
		$(document).ready(function() {
			$.jgrid.defaults.styleUI = "Bootstrap";
			$("#table_list").jqGrid({
				url: '<%=contextPath %>/functional/getTrees',
				data : {},
				datatype : "json",
				autowidth : true,
				shrinkToFit : true,
				mtype : "post",
				rowNum : 20,
				rowList : [ 10, 20, 30 ],
				colNames : ["id", "职能名称","排序","操作"],
				colModel : [{
					name : "id",
					index : "id",
					hidden:true
				},  {
					name : "functional.name",
					index : "functional.name",
					width : 100
				},  {
					name : "functional.sort",
					index : "functional.sort",
					width : 100
				},{
					formatter:function(cellvalue, options, row){
						if(row.level==1){
					     return  "<s:hasPermission name="functional:add"><a class='btn btn-mini btn-success' title='添加二级' onclick='addfunctional1(&quot;"+row.id+"&quot;)'><i class='icon-plus'>添加二级</i></a></s:hasPermission> "
					     +"<s:hasPermission name="functional:detail"><a class='btn btn-small btn-info' title='查看' onclick='showdetail(&quot;"+row.id+"&quot;)'><i class='icon-eye-open'></i></a></s:hasPermission> "
					         +" <s:hasPermission name="functional:edit"><a class='btn btn-mini btn-warning' title='修改' onclick='showedit(&quot;"+row.id+"&quot;)'><i class='icon-edit'></i></a></s:hasPermission> "
							   +" <s:hasPermission name="functional:delete"><a class='btn btn-mini btn-danger' title='删除' onclick='del(&quot;"+row.id+"&quot;)'><i class='icon-trash'></i></a></s:hasPermission> ";
						}else if(row.level==2){
							return  "<s:hasPermission name="functional:add"><a class='btn btn-mini btn-success' title='添加三级' onclick='addfunctional2(&quot;"+row.id+"&quot;)'><i class='icon-plus'>添加三级</i></a></s:hasPermission> "
						     +"<s:hasPermission name="functional:detail"><a class='btn btn-small btn-info' title='查看' onclick='showdetail(&quot;"+row.id+"&quot;)'><i class='icon-eye-open'></i></a></s:hasPermission> "
						         +" <s:hasPermission name="functional:edit"><a class='btn btn-mini btn-warning' title='修改' onclick='showedit(&quot;"+row.id+"&quot;)'><i class='icon-edit'></i></a></s:hasPermission> "
								   +" <s:hasPermission name="functional:delete"><a class='btn btn-mini btn-danger' title='删除' onclick='del(&quot;"+row.id+"&quot;)'><i class='icon-trash'></i></a></s:hasPermission> ";
						}else{
							return  "<s:hasPermission name="functional:detail"><a class='btn btn-small btn-info' title='查看' onclick='showdetail(&quot;"+row.id+"&quot;)'><i class='icon-eye-open'></i></a></s:hasPermission> "
						         +" <s:hasPermission name="functional:edit"><a class='btn btn-mini btn-warning' title='修改' onclick='showedit(&quot;"+row.id+"&quot;)'><i class='icon-edit'></i></a></s:hasPermission> "
								   +" <s:hasPermission name="functional:delete"><a class='btn btn-mini btn-danger' title='删除' onclick='del(&quot;"+row.id+"&quot;)'><i class='icon-trash'></i></a></s:hasPermission> ";
						}
						},
					width : 150
				}],
				 treeReader : {  
					  level_field: "level",  
				      parent_id_field: "parent",   
				      leaf_field: "leaf",  
				      expanded_field: "expanded"  
				    },  
			    jsonReader: {      
			        root: "rows",    
			        repeatitems : false      
			      },   
				ExpandColumn: "functional.name",
				treeGrid: true,
				height : $(window).height() - 130,
				treeGridModel: "adjacency"
			});
			
			//表格自适应高宽
			$(window).bind("resize", function() {
				var b = $(".jqGrid_wrapper").width();
				$("#table_list").setGridWidth(b);
				$("#table_list").setGridHeight($(window).height() - 170);
			})
			
			$("#table_list")
			.navGrid('#pager_list',{edit:false,add:false,del:false,search:false})
			$("#find_btn").click(function(){ 
		        var uname = $("#uname").val(); 
		        $("#table_list").jqGrid('setGridParam',{ 
		            postData:{'functionalName':uname}, //发送数据 
		            page:1 
		        }).trigger("reloadGrid"); //重新载入 
		    }); 
			
		});
		function showdetail(id){
			 openyem("查看职能","<%=contextPath %>/functional/detailView?id="+id,550,500);//查看页面
		}
		function add(){
			 openyem("添加职能","<%=contextPath %>/functional/addView?level=1",550,500);//打开页面
		}
		function addfunctional1(id){
			 openyem("添加二级职能","<%=contextPath %>/functional/addView?pid="+id+"&level=2",550,500);//打开页面
		}
		function addfunctional2(id){
			 openyem("添加三级职能","<%=contextPath %>/functional/addView?pid="+id+"&level=3",550,500);//打开页面
		}
		//删除
		function del(id){
			swalopen(id,"<%=contextPath %>/functional/delete");
		};
		
		//修改
		function showedit(id){
			openyem("修改职能","<%=contextPath %>/functional/editView?id=" + id, 550, 500);
		}
		function uploadxlsx(){
		     $('#xlsfile').click();
		}
		 function inputExcel(){
			  $("#signupForm").ajaxSubmit({
		          url: '<%=contextPath%>/functional/inputExcel',
		          type: 'post',
		          dataType: "json",
		          success: function (data) {
		  				if(data.code==200){
		  					toastr.success("导入数据成功");
		  					$("#table_list").trigger("reloadGrid");
		  					
		  		    	}else{
		  		    		toastr.error("模板格式错误或数据格式错误！请在本页导出模板，根据模板填写数据。");
		  		    	}
		  			 },
		  			error:function(){
		  				toastr.error("未知错误，请稍后再试！");
		  			}
		      });
		  }
	</script>

</body>
</html>

